﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using CFrame.Common;
using CFrame.Tools;
using CFrame.Manager;

/*
 * 脚本功能：网络消息处理基类
 * 编写日期：2018.12.13
 * 编写人：HYZ
 */
namespace CFrame.Request
{
    public abstract class BaseRequest:MonoBehaviour
    {
        protected int requestCode;
        protected int actionCode;
        private GameMiddle _game;
        protected GameMiddle game
        {
            get
            {
                if(_game == null)
                {
                    _game = GameMiddle.Instance;
                }
                return _game;
            }
        }

        public void Awake()
        {
            //注册网络数据监听事件
            RegisterRequest();
        }

        /// <summary>
        /// 注册Request事件
        /// </summary>
        protected abstract void RegisterRequest();
        
        /// <summary>
        /// 移除注册事件
        /// </summary>
        protected abstract void RemoveRequest();

        /// <summary>
        /// 发送消息方法
        /// </summary>
        /// <param name="data"></param>
        protected void SendRequest(string data)
        {
            try {

                game.GetManager<NetManager>().SendRequest(requestCode, actionCode, data);

            } catch(Exception e)
            {
                Debuger.LogError("NetManager 没有注册，发送数据失败");
            }
        }

        /// <summary>
        /// 当接收到数据时调用
        /// </summary>
        /// <param name="data"></param>
        public virtual void OnResponse(string data)
        {
            Debuger.Log("【OnResponse】 --> " + data);
        }


        public virtual void OnDestroy()
        {
            RemoveRequest();
        }
    }
}
